package com.zxs.seckillservice.conf;

import com.zxs.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Configuration
public class RabbaitMQConfig {


    /**
     * 订单交换机
     * @return
     */
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(Constants.EXCHANGE_NAME_ORDER, true, false);
    }


    /**
     * 订单队列
     * @return
     */
    @Bean
    public Queue orderQueue() {
        return new Queue(Constants.QUEUE_NAME_ORDER, true);
    }


    /**
     * 订单队列绑定交换机
     * @return
     */
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(Constants.ROUTING_KEY_ORDER);
    }


    /**
     * 声明延迟交换机
     * @return
     */
    @Bean
    public CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(Constants.EXCHANGE_DELAY_NAME, "x-delayed-message", true, false, args);
    }

    /**
     * 延迟队列
     * @return
     */
    @Bean
    public Queue dalayQueue() {
        return new Queue(Constants.QUEUE_DELAY_NAME, true);
    }

    /**
     * 绑定延迟队列
     * @return
     */
    @Bean
    public Binding bindingDelayQueue() {
        return BindingBuilder.bind(dalayQueue()).to(customExchange()).with(Constants.ROUTING_KEY_DELAY_NAME).noargs();
    }

}
